--[[=========================================================
	DATA MANAGEMENT FUNCTIONS
=========================================================]]--
if SERVER then

--[[-----------------------------------------------------]]--
--	Generic update statement
--[[-----------------------------------------------------]]--
sql.Update						= function( tab, ids, ... )

	--	Check if the row already exists
	local idString				= sql.GetSpecificValueString( ids, ... )
	local result				= sql.Query( sql.GetSelect( tab, ids, ... ) )
	
	--	Row exists, update instead of inserting
	if not result then
		DEBUG:Msg( "Record '" .. idString .. "' not found in table '" .. tostring( tab ) .. "'. Inserting...", "sql.Update" )
		return sql.Insert( tab, ids, ... )
		
	--	Update
	else
		
		--	Prepare & run update
		result					= sql.Query( sql.GetUpdate( tab, ids, ... ) )
		
		--	Warn on error
		if IsSQLError( result ) then
			DEBUG:ErrMsg( "An error occurred while trying to update '" .. idString .. "' in table '" .. tostring( tab ) .. "'. Last error: " .. tostring( sql.LastError() ), "sql.Update" )
			
		--	Notify success
		else
			DEBUG:Msg( "The record '" .. idString .. "' was successfully updated in table '" .. tostring( tab ) .. "'", "sql.Update" )
		end
	end

	--	Return the result
	return result
end

--[[-----------------------------------------------------]]--
--	Generic insert statement
--[[-----------------------------------------------------]]--
sql.Insert						= function( tab, ids, ... )

	--	Check if the row already exists
	local idString				= sql.GetSpecificValueString( ids, ... )
	local result				= sql.Query( sql.GetSelect( tab, ids, ... ) )
	
	--	Row not found, insert instead of updating
	if result then
		DEBUG:Msg( "Record '" .. idString .. "' exists in table '" .. tostring( tab ) .. "'. Updating...", "sql.Insert" )
		return sql.Update( tab, ids, ... )
		
	--	Insert
	else
	
		--	Check validity of id fields
		for _,v in ipairs( ids ) do
		
			--	Can't insert if a part of the PK is not set
			if tonumber( v ) == 0 then
				DEBUG:ErrMsg( "Can't insert '" .. idString .. "' into table '" .. tostring( tab ) .. "' because a part of the PK is not set", "sql.Insert" )
				return false
			end
		end
		
		--	Prepare & run insert
		result					= sql.Query( sql.GetInsert( tab, ... ) )
		
		--	Warn on error
		if IsSQLError( result ) then
			DEBUG:ErrMsg( "An error occurred while trying to insert '" .. idString .. "' in table '" .. tostring( tab ) .. "'. Last error: " .. tostring( sql.LastError() ), "sql.Insert" )
			
		--	Notify success
		else
			DEBUG:Msg( "The record '" .. idString .. "' was successfully inserted in table '" .. tostring( tab ) .. "'", "sql.Insert" )
		end
	end

	--	Return the result
	return result
end

--[[-----------------------------------------------------]]--
--	Generic delete statement
--[[-----------------------------------------------------]]--
sql.Delete						= function( tab, ids, ... )

	--	Check if the row already exists
	local idString				= sql.GetSpecificValueString( ids, ... )
	local result				= sql.Query( sql.GetSelect( tab, ids, ... ) )
	
	--	Row doesn't exist, nothing do delete
	if not result then
		DEBUG:Msg( "Record '" .. idString .. "' not found in table '" .. tostring( tab ) .. "'. Skipping...", "sql.Delete" )
		return result
		
	--	Delete
	else
		
		--	Prepare & run delete
		result					= sql.Query( sql.GetDelete( tab, ids, ... ) )
		
		--	Warn on error
		if IsSQLError( result ) then
			DEBUG:ErrMsg( "An error occurred while trying to delete '" .. idString .. "' from table '" .. tostring( tab ) .. "'. Last error: " .. tostring( sql.LastError() ), "sql.Delete" )
			
		--	Notify success
		else
			DEBUG:Msg( "The record '" .. idString .. "' was successfully deleted from table '" .. tostring( tab ) .. "'", "sql.Delete" )
		end
	end

	--	Return the result
	return result
end

--[[-----------------------------------------------------]]--
--	Generic get statement
--[[-----------------------------------------------------]]--
sql.GetRow						= function( tab, ids, ... )

	--	Check if the row already exists
	local idString				= sql.GetSpecificValueString( ids, ... )
	local result				= sql.Query( sql.GetSelect( tab, ids, ... ) )
	
	--	Row doesn't exist, warn in console
	if not result then
		DEBUG:Msg( "Record '" .. idString .. "' not found in table '" .. tostring( tab ) .. "'. Nothing to return...", "sql.Reload" )
		
	--	Notify success
	else
		DEBUG:Msg( "The record '" .. idString .. "' was successfully retrieved from table '" .. tostring( tab ) .. "'", "sql.Reload" )	
	end

	--	Return the result
	if not result then return result
	else return result[1] end
end

--[[-----------------------------------------------------]]--
--	Generates a new ID for the specified table
--[[-----------------------------------------------------]]--
sql.GetNewID					= function( tab, fld )

	--	Must have a FieldInfo
	if IsValidInstance( fld, FieldInfo ) then
	
		--	Field must be a number
		if fld.Type != "number" then
			DEBUG:ErrMsg( "Field has to be a number.", "sql.GetNewID" )
			return 0
		end
		
		--	Count records in table
		local count				= sql.Query( "SELECT COUNT(*) AS 'CNT' FROM " .. tostring( tab ) )
		
		--	Return next ID
		return tonumber( count[1]["CNT"] ) + 1
	
	--	Otherwise return invalid ID
	else
		DEBUG:ErrMsg( "'FieldInfo' required as #2 parameter.", "sql.GetNewID") 
		return 0
	end

end

end